from PySide2.QtWidgets import *
from PySide2.QtCore import QFile, QStringListModel, Qt
from PySide2.QtUiTools import QUiLoader

from ExcelData import EData, ExcelData, TableData, SaveType


class SettingDialog:
    edata: EData

    txtCfgSheetName: QLineEdit
    txtTagImportSheets: QLineEdit
    txtCfgIgnoreColumn: QLineEdit

    txtDirExcelFile: QLineEdit
    txtDirJsonFile: QLineEdit
    txtDefineJsonFile: QLineEdit
    txtDirJavaFile: QLineEdit
    txtDirLayaFile: QLineEdit
    txtDirUnityFile: QLineEdit

    txtLibConstantClass: QLineEdit
    txtLibEasyAccessClass: QLineEdit
    txtLibPrefix: QLineEdit

    def __init__(self):
        super(SettingDialog, self).__init__()

        self.edata = EData()

        # 从文件中加载UI定义
        qfile = QFile("data\\setting.ui")
        qfile.open(QFile.ReadOnly)
        qfile.close()
        # 从UI定义中动态创建一个相应的窗口对象, 将其内部文件定义为self.ui.button
        self.ui = QUiLoader().load(qfile)

        self.txtCfgSheetName = self.ui.txtCfgSheetName
        self.txtTagImportSheets = self.ui.txtTagImportSheets
        self.txtCfgIgnoreColumn = self.ui.txtCfgIgnoreColumn

        self.txtDirExcelFile = self.ui.txtDirExcelFile
        self.txtDirJsonFile = self.ui.txtDirJsonFile
        self.txtDefineJsonFile = self.ui.txtDefineJsonFile
        self.txtDirJavaFile = self.ui.txtDirJavaFile
        self.txtDirLayaFile = self.ui.txtDirLayaFile
        self.txtDirUnityFile = self.ui.txtDirUnityFile

        self.txtLibConstantClass = self.ui.txtLibConstantClass
        self.txtLibEasyAccessClass = self.ui.txtLibEasyAccessClass
        self.txtLibPrefix = self.ui.txtLibPrefix

        # 绑定按钮
        self.ui.btnDefault.clicked.connect(self.onBthDefaultClick)
        self.ui.btnOk.clicked.connect(self.onBthOkClick)
        self.ui.btnClose.clicked.connect(self.onBthCloseClick)

    def show(self):
        print("打开配置UI")
        self.loadFromData()
        self.ui.show()

    def loadFromData(self):
        self.txtCfgSheetName.setText(self.edata.setting.readerCfgSheetName)
        self.txtTagImportSheets.setText(self.edata.setting.readerCfgTagImportSheets)
        self.txtCfgIgnoreColumn.setText(self.edata.setting.readerCfgIgnoreColumn)

        self.txtDirExcelFile.setText(self.edata.setting.defDirExcelFile)
        self.txtDirJsonFile.setText(self.edata.setting.defDirJsonFile)
        self.txtDefineJsonFile.setText(self.edata.setting.defDefineJsonFile)
        self.txtDirJavaFile.setText(self.edata.setting.defDirJavaFile)
        self.txtDirLayaFile.setText(self.edata.setting.defDirLayaFile)
        self.txtDirUnityFile.setText(self.edata.setting.defDirUnityFile)

        self.txtLibConstantClass.setText(self.edata.setting.genLibConstantClass)
        self.txtLibEasyAccessClass.setText(self.edata.setting.genLibEasyAccessClass)
        self.txtLibPrefix.setText(self.edata.setting.genLibPrefix)

    def saveFromData(self):
        self.edata.setting.readerCfgSheetName = self.txtCfgSheetName.text()
        self.edata.setting.readerCfgTagImportSheets = self.txtTagImportSheets.text()
        self.edata.setting.readerCfgIgnoreColumn = self.txtCfgIgnoreColumn.text()

        self.edata.setting.defDirExcelFile = self.txtDirExcelFile.text()
        self.edata.setting.defDirJsonFile = self.txtDirJsonFile.text()
        self.edata.setting.defDefineJsonFile = self.txtDefineJsonFile.text()
        self.edata.setting.defDirJavaFile = self.txtDirJavaFile.text()
        self.edata.setting.defDirLayaFile = self.txtDirLayaFile.text()
        self.edata.setting.defDirUnityFile = self.txtDirUnityFile.text()

        self.edata.setting.genLibConstantClass = self.txtLibConstantClass.text()
        self.edata.setting.genLibEasyAccessClass = self.txtLibEasyAccessClass.text()
        self.edata.setting.genLibPrefix = self.txtLibPrefix.text()

    def onBthDefaultClick(self):
        self.edata.setting.resetDefault(True)
        self.loadFromData()

    def onBthOkClick(self):
        print("保存设置")
        self.saveFromData()
        self.edata.save(SaveType.Setting)
        self.ui.close()

    def onBthCloseClick(self):
        self.ui.close()
